package com.weirong.smartvault.backservice.util.common;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * 于2017/12/18由 KFEB4 创建 .
 * 统计工具
 */
public class StatisticsUtil {
    public static List selectByDate(List list, String dateParam, Integer dateUnit) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        //每个时间区间内选择一个重新组成列表后返回
        list.sort((o1, o2) -> {
            try {
                Date t1 = (Date) ReflectTool.runMethod(o1, dateParam);
                Date t2 = (Date) ReflectTool.runMethod(o2, dateParam);
                return (t1.getTime()-t2.getTime())>0?1:0;
            }catch (Exception e){
                e.printStackTrace();
            }
            return 0;
        });
        List res = new ArrayList();
        Object last = list.get(0);
        res.add(last);
        for (Object obj :list){
            Date d1 = (Date) ReflectTool.runMethod(obj, dateParam);
            Date d2 = (Date) ReflectTool.runMethod(last, dateParam);
            if (!DateUtil.compareDateInUnit(d1,d2,dateUnit)){
                res.add(obj);
                last = obj;
            }
        }
        return res;
    }
    public static Object pickByDate(List list,String dataparam,Integer dataUnit,Integer goal) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        for (Object obj:list){
            Date date = (Date) ReflectTool.runMethod(obj,dataparam);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if (calendar.get(dataUnit)==goal){
                return obj;
            }
        }
        return null;
    }
}
